#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
#include <memory>
#include <sstream>
#include <map>

class Cpp_Next {
public:
    using Function = std::function<void(const std::vector<std::string>&)>;

    Cpp_Next() {
        defineFunction("let", [this](const std::vector<std::string>& args) {
            if (args.size() >= 2) {
                std::string varName = args[0];
                auto value = std::make_shared<std::string>(args[1]); // 使用智能指针
                currentModule()->variables_[varName] = value;
            }
        });

        defineFunction("print", [this](const std::vector<std::string>& args) {
            for (const auto& arg : args) {
                if (currentModule()->variables_.count(arg)) {
                    std::cout << *currentModule()->variables_[arg] << ' '; // 访问智能指针的值
                } else {
                    std::cout << arg << ' ';
                }
            }
            std::cout << '\n';
        });

        defineFunction("module", [this](const std::vector<std::string>& args) {
            if (args.size() == 1) {
                currentModuleName_ = args[0];
                if (modules_.count(currentModuleName_) == 0) {
                    modules_[currentModuleName_] = std::make_shared<Module>();
                }
            }
        });

        defineFunction("import", [this](const std::vector<std::string>& args) {
            if (args.size() == 1) {
                currentModuleName_ = args[0];
                if (modules_.count(currentModuleName_) == 0) {
                    std::cerr << "Module " << currentModuleName_ << " not found.\n";
                }
            }
        });
    }

    void defineFunction(const std::string& name, Function func) {
        functions_[name] = func;
    }

    void run(const std::string& code) {
        auto tokens = tokenize(code);
        executeCommand(tokens, 0);
    }

private:
    struct Module {
        std::unordered_map<std::string, std::shared_ptr<std::string>> variables_;
    };

    std::vector<std::string> tokenize(const std::string& code) {
        std::vector<std::string> tokens;
        std::string token;
        for (char ch : code) {
            if (isspace(ch)) {
                if (!token.empty()) {
                    tokens.push_back(token);
                    token.clear();
                }
            } else {
                token += ch;
            }
        }
        if (!token.empty()) tokens.push_back(token);
        return tokens;
    }

    void executeCommand(const std::vector<std::string>& tokens, size_t start) {
        if (tokens.empty() || start >= tokens.size()) return;

        std::string command = tokens[start];
        if (functions_.count(command)) {
            functions_[command](std::vector<std::string>(tokens.begin() + start + 1, tokens.end()));
        }
    }

    Module* currentModule() {
        return modules_[currentModuleName_].get();
    }

    std::unordered_map<std::string, Function> functions_;
    std::unordered_map<std::string, std::shared_ptr<Module>> modules_;
    std::string currentModuleName_ = "global";
};

int main() {
    Cpp_Next script;

    // 模块化和封装特性示例
    script.run("module math");
    script.run("let pi 3.14");
    script.run("print pi");

    script.run("module main");
    script.run("import math");
    script.run("print pi");

    return 0;
}

